﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using neside3.ext;

namespace neside3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, ext.ILogger
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        DispatcherTimer timer;
        SolidColorBrush colorBorder;
        SolidColorBrush colorLogo;
        Color nextColor;
        Border logo;

        Border area_func;//功能区
        GroupBox area_result;//编译结果区
        TabControl area_editor;//多标签编辑区
        ListBox output_list;//输出列表

        TextBlock bottom_bar_text;//底栏文字
        StackPanel bottom_bar_tray;//底栏托盘
        float borderTimer;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            output_list = this.Template.FindName("output_list", this) as ListBox;
            output_list.Items.Clear();


            //初始化定时器
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(20);
            timer.Tick += onTick;
            timer.Start();

            //初始化所有交互区域
            area_func = this.Template.FindName("area_func", this) as Border;
            area_result = this.Template.FindName("area_result", this) as GroupBox;
            area_editor = this.Template.FindName("area_editor", this) as TabControl;
            bottom_bar_text = this.Template.FindName("bottom_bar_text", this) as TextBlock;
            bottom_bar_tray = this.Template.FindName("bottom_bar_tray", this) as StackPanel;

            area_func.Background = null;
            area_func.Child = null;

            area_result.Content = null;
            area_editor.Items.Clear();
            bottom_bar_text.Text = "done";
            bottom_bar_tray.Children.Clear();

            //初始化logo
            logo = this.Template.FindName("top_logo", this) as Border;
            colorLogo = new SolidColorBrush();
            colorLogo.Color = Color.FromArgb(255, 255, 0, 0);
            nextColor = Color.FromArgb(255, 0, 255, 0);

            //初始化border
            colorBorder = new SolidColorBrush();
            colorBorder.Color = Color.FromArgb(255, 0x30, 0x5f, 0xfd);
            borderTimer = 0;

            //需要输出，先初始化list才可以
            IDE.InitPlugin(this);
            IDE.Documents.Init(this.area_editor);
            IDE.Builder.Init(this.area_result);
            InitEditMode();
        }
        SolidColorBrush colorLog = new SolidColorBrush(Color.FromRgb(255, 255, 255));
        SolidColorBrush colorWarn = new SolidColorBrush(Color.FromRgb(255, 255, 0));
        SolidColorBrush colorError = new SolidColorBrush(Color.FromRgb(255, 0, 0));
        void ILogger.Log(string str)
        {
            Action p0 = () =>
             {
                 object txt = new TextBlock() { Text = str, Foreground = colorLog };
                 this.output_list.Items.Add(txt);
             };

            this.Dispatcher.Invoke(p0);
        }

        void ILogger.Warn(string str)
        {
            Action p0 = () =>
            {
                object txt = new TextBlock() { Text = str, Foreground = colorWarn };
                this.output_list.Items.Add(txt);
            };

            this.Dispatcher.Invoke(p0);
        }

        void ILogger.Error(string str)
        {
            Action p0 = () =>
            {
                object txt = new TextBlock() { Text = str, Foreground = colorError };
                this.output_list.Items.Add(txt);
            };

            this.Dispatcher.Invoke(p0);
        }

        void ILogger.Clear()
        {
            this.Dispatcher.Invoke(this.output_list.Items.Clear);
        }
        void onTick(object sender, EventArgs e)
        {
            loopBorderColor();
            loopLogoColor();
        }
        private void loopBorderColor()
        {
            borderTimer += 0.01f;
            if (borderTimer > 1.0f)
                borderTimer -= 1.0f;
            var alpha = borderTimer > 0.5f ? (0.5f + 1.0f - borderTimer) : (0.5f + borderTimer);
            var nc = colorBorder.Color;
            nc.A = (byte)(alpha * 255);
            colorBorder.Color = nc;
            this.BorderBrush = colorBorder;
        }
        private void loopLogoColor()
        {
            var color = colorLogo.Color;
            if (color == nextColor)
            {
                if (nextColor == Color.FromArgb(255, 255, 0, 0))
                {
                    nextColor = Color.FromArgb(255, 0, 255, 0);
                }
                else if (nextColor == Color.FromArgb(255, 0, 255, 0))
                {
                    nextColor = Color.FromArgb(255, 0, 0, 255);
                }
                else
                {
                    nextColor = Color.FromArgb(255, 255, 0, 0);
                }
            }

            //neart to nextColor
            for (var i = 0; i < 5; i++)
            {
                if (color.R < nextColor.R)
                    color.R++;
                else if (color.R > nextColor.R)
                    color.R--;
                if (color.G < nextColor.G)
                    color.G++;
                else if (color.G > nextColor.G)
                    color.G--;
                if (color.B < nextColor.B)
                    color.B++;
                else if (color.B > nextColor.B)
                    color.B--;
            }
            colorLogo.Color = color;
            logo.Background = colorLogo;
            logo.UpdateLayout();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {//func_edit_click
            //edit 模式 偏蓝
            InitEditMode();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {//func_play_click
            //play 模式 偏红
            InitPlayMode();
        }
        private void InitEditMode()
        {
            colorBorder.Color = Color.FromArgb(255, 0x30, 0x5f, 0xfd);
            area_func.Child = IDE.getUI_func_edit();
        }
        private void InitPlayMode()
        {
            colorBorder.Color = Color.FromArgb(255, 0xfd, 0x5f, 0x30);
            area_func.Child = IDE.getUI_func_play();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            ShowAbout();
        }
        private void ShowAbout()
        {
            Dialog_About about = new Dialog_About();
            about.ShowDialog();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //Save current File
            IDE.Documents.SaveCurrentFile();

        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //Save All File
            IDE.Documents.SaveAllFile();
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            //Close All File
            IDE.Documents.CloseAllFile();
        }
    }
}
